package cn.yunhe.service.impl;

import cn.yunhe.dao.IbranchDao;
import cn.yunhe.entity.Branch;
import cn.yunhe.entity.Emp;
import cn.yunhe.service.IbranchService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 姜威
 * @date 2018/9/14 16:37
 */

@Service("branchService")
public class BranchService implements IbranchService {

    @Resource
    private IbranchDao branchDao;

    @Override
    public Page<Branch> findLikeBranchs(Map<String, Object> map) {
        Page<Branch> page=new Page<Branch>();//初始化page类

        page.setTotal(branchDao.findLikeBranchsCount(map));//获得总记录数

        int pageNo=Integer.parseInt(String.valueOf(map.get("pageNo")));//获得当前页
        int pageSize=Integer.parseInt(String.valueOf(map.get("pageSize")));

        page.setPageNum((int) (page.getTotal()/pageSize+(page.getTotal()%pageSize==0?0:1)));
        page=PageHelper.startPage(pageNo,pageSize);
        List<Branch> list=branchDao.findLikeBranchs(map);
        return page;
    }

    @Override
    public List<Branch> findAllBranch() {
        return branchDao.findAllBranch();
    }

    @Override
    public List<Emp> findUserByUname(String uname) {
        return branchDao.findUserByUname(uname);
    }


    @Override
    public Branch findBranchByBuuid(String buuid) {
           Branch branch=branchDao.findBranchByBuuid(buuid);
           List<String> list=branchDao.findEmpsByBuuid(buuid);
            Calendar cal=Calendar.getInstance();
            String year=String.valueOf(cal.get(Calendar.YEAR));
            Map<String,Object> map1=new HashMap<String, Object>();
            map1.put("start",(year+"-"+"01"));
            map1.put("end",(year+"-"+"03"));
            Map<String,Object> map2=new HashMap<String, Object>();
            map2.put("start",(year+"-"+"04"));
            map2.put("end",(year+"-"+"06"));
            Map<String,Object> map3=new HashMap<String, Object>();
            map3.put("start",(year+"-"+"07"));
            map3.put("end",(year+"-"+"09"));
            Map<String,Object> map4=new HashMap<String, Object>();
            map4.put("start",(year+"-"+"10"));
            map4.put("end",(year+"-"+"12"));
            double quarter1=0.0;
            double quarter2=0.0;
            double quarter3=0.0;
            double quarter4=0.0;
            for (String euuid:list) {
                map1.put("euuid", euuid);
                map2.put("euuid", euuid);
                map3.put("euuid", euuid);
                map4.put("euuid", euuid);
                String result1 = branchDao.findOrdrPayMoneyByEuuid(map1);
               /* System.out.println(result1);*/
                String result5 = branchDao.findCasePaycountByEuuid(map1);
                /*System.out.println(result5);*/
                String result2 = branchDao.findOrdrPayMoneyByEuuid(map2);
               /* System.out.println(result2);*/
                String result6 = branchDao.findCasePaycountByEuuid(map2);
                /*System.out.println(result6);*/
                String result3 = branchDao.findOrdrPayMoneyByEuuid(map3);
                /*System.out.println(result3);*/
                String result7 = branchDao.findCasePaycountByEuuid(map3);
              /*  System.out.println(result7);*/
                String result4 = branchDao.findOrdrPayMoneyByEuuid(map4);
               /* System.out.println(result4);*/
                String result8 = branchDao.findCasePaycountByEuuid(map4);
               /* System.out.println(result8);*/
                if (result1 != null && result5 != null) {
                    quarter1 += Double.parseDouble(branchDao.findOrdrPayMoneyByEuuid(map1)) - Double.parseDouble(branchDao.findCasePaycountByEuuid(map1));
                }
                if (result1 != null && result5 == null) {
                    quarter1 += Double.parseDouble(branchDao.findOrdrPayMoneyByEuuid(map1));
                }
                if (result2 != null && result6 != null) {
                    quarter2 += Double.parseDouble(branchDao.findOrdrPayMoneyByEuuid(map2)) - Double.parseDouble(branchDao.findCasePaycountByEuuid(map2));
                }
                if (result2 != null && result6 == null) {
                    quarter2 += Double.parseDouble(branchDao.findOrdrPayMoneyByEuuid(map2));
                }
                if (result3 != null && result7 != null) {
                    quarter3 += Double.parseDouble(branchDao.findOrdrPayMoneyByEuuid(map3)) - Double.parseDouble(branchDao.findCasePaycountByEuuid(map3));
                }
                if (result3 != null && result7 == null) {
                    quarter3 += Double.parseDouble(branchDao.findOrdrPayMoneyByEuuid(map3));
                }
                if (result4 != null && result8 != null) {
                    quarter4 += Double.parseDouble(branchDao.findOrdrPayMoneyByEuuid(map4)) - Double.parseDouble(branchDao.findCasePaycountByEuuid(map4));
                }
                if (result4 != null && result8 == null) {
                    quarter4 += Double.parseDouble(branchDao.findOrdrPayMoneyByEuuid(map4));
                }
            }
            branch.setQuarter1(quarter1/10000);
            branch.setQuarter2(quarter2/10000);
            branch.setQuarter3(quarter3/10000);
            branch.setQuarter4(quarter4/10000);
            return branch;
    }

    @Override
    public boolean addBranch(Branch branch) {
        return branchDao.addBranch(branch);
    }

    @Override
    public boolean updateBranch(Branch branch) {
        return branchDao.updateBranch(branch);
    }

    @Override
    public boolean delBranch(Branch branch) {
        return branchDao.delBranch(branch);
    }

    public IbranchDao getBranchDao() {
        return branchDao;
    }

    public void setBranchDao(IbranchDao branchDao) {
        this.branchDao = branchDao;
    }
}
